﻿using System;
using DBProvider.Providers;
using System.Data.Linq;
using System.Web.UI.WebControls;
using DBProvider;
using System.Collections.Generic;

namespace HealthCommunity.Controls
{
	public partial class UCVideoList : System.Web.UI.UserControl
	{
		private VideoProvider _VideoProvider;

		private IList<Video> _DataSource;
		private int _TotalRecords;

		private IList<Video> _DataSource1;
		private int _TotalRecords1;

		public IList<Video> DataSourse
		{
			get
			{
				var pageIndex = this.PageIndex;
				var totalRecords = 0;
				var pageSize = this.PageSize;

				var u = SiteContext.Current.CurrentUser;
				if(u != null)
				{
					if(_VideoProvider == null)
					{
						_VideoProvider = new VideoProvider();
					}

					_DataSource = _VideoProvider.GetVideoByUser(u.UserID, pageIndex, pageSize, out totalRecords);
				}

				this.TotalRecords = totalRecords;

				return _DataSource;
			}
		}

		public int PageIndex
		{
			get
			{
				if(ViewState["PageIndex"] == null)
				{
					return 0;
				}
				else
				{
					return int.Parse(ViewState["PageIndex"].ToString());
				}
			}
			set
			{
				ViewState["PageIndex"] = value;
			}
		}

		public int TotalRecords
		{
			get
			{
				return _TotalRecords;
			}
			set
			{
				_TotalRecords = value;
			}
		}

		public int TotalPages
		{
			get
			{
				var data = _DataSource == null ? this.DataSourse : _DataSource;

				if(data == null)
				{
					return 0;
				}

				int totalRecords = TotalRecords;

				int totalPages = 0;

				if(totalRecords <= PageSize)
				{
					totalPages = 1;
				}

				if(totalRecords % PageSize == 0)
				{
					totalPages = totalRecords / PageSize;
				}

				if(totalRecords % PageSize > 0)
				{
					totalPages = totalRecords / PageSize + 1;
				}

				return totalPages;
			}
		}

		public int PageSize
		{
			get
			{
				return int.Parse(ddlPageSize.SelectedValue);
			}
		}

		public IList<Video> DataSourse1
		{
			get
			{
				var pageIndex = this.PageIndex1;
				var totalRecords = 0;
				var pageSize = this.PageSize1;

				var u = SiteContext.Current.CurrentUser;

				if(u != null && u.Role.RoleName.ToLower().Equals("admin"))
				{
					if(_VideoProvider == null)
					{
						_VideoProvider = new VideoProvider();
					}

					var drugStore = SiteContext.Current.CurrentDrugStore;

					if(drugStore != null)
					{
						_DataSource1 = _VideoProvider.GetVideoExcludeUser(u.UserID, drugStore.DrugStoreID, pageIndex, pageSize, out totalRecords);
					}
				}
				else
				{
					if(u != null && u.DrugStoreID.GetValueOrDefault(0) > 0)
					{
						if(_VideoProvider == null)
						{
							_VideoProvider = new VideoProvider();
						}

						_DataSource1 = _VideoProvider.GetVideoExcludeUser(u.UserID, u.DrugStoreID.Value, pageIndex, pageSize, out totalRecords);
					}
				}

				this.TotalRecords1 = totalRecords;

				return _DataSource1;
			}
		}

		public int PageIndex1
		{
			get
			{
				if(ViewState["PageIndex1"] == null)
				{
					return 0;
				}
				else
				{
					return int.Parse(ViewState["PageIndex1"].ToString());
				}
			}
			set
			{
				ViewState["PageIndex1"] = value;
			}
		}

		public int TotalRecords1
		{
			get
			{
				return _TotalRecords1;
			}
			set
			{
				_TotalRecords1 = value;
			}
		}

		public int TotalPages1
		{
			get
			{
				var data = _DataSource1 == null ? this.DataSourse1 : _DataSource1;

				if(data == null)
				{
					return 0;
				}

				int totalRecords = TotalRecords1;

				int totalPages = 0;

				if(totalRecords <= PageSize1)
				{
					totalPages = 1;
				}

				if(totalRecords % PageSize1 == 0)
				{
					totalPages = totalRecords / PageSize1;
				}

				if(totalRecords % PageSize1 > 0)
				{
					totalPages = totalRecords / PageSize1 + 1;
				}

				return totalPages;
			}
		}

		public int PageSize1
		{
			get
			{
				return int.Parse(ddlPageSize1.SelectedValue);
			}
		}

		public string RoleName
		{
			get
			{
				var result = string.Empty;

				var u = SiteContext.Current.CurrentUser;

				if(u != null)
				{
					if(u.Role != null)
					{
						result = u.Role.RoleName;
					}
				}

				return result;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void lvPublicVideos_ItemCommand(object sender, ListViewCommandEventArgs e)
		{
			if(e.CommandName == "remove")
			{
				var id = int.Parse(e.CommandArgument.ToString());

				if(_VideoProvider == null)
				{
					_VideoProvider = new VideoProvider();
				}

				if(_VideoProvider.DeleteVideo(id))
				{
					//BindPublicVideo();
				}
			}
		}

		protected void lvPublicVideos_ItemDataBound(object sender, ListViewItemEventArgs e)
		{
			var lbtnDelete = e.Item.FindControl("lbtnDelete") as LinkButton;

			if(!string.IsNullOrEmpty(RoleName))
			{
				lbtnDelete.Visible = (RoleName.Equals("admin") || RoleName.Equals("storemanager") || RoleName.Equals("storeadmin"));
			}
			else
			{
				lbtnDelete.Visible = false;
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindMyVideos();
			BindPublicVideo();

			var currentPageIndex = this.PageIndex;
			var totalPages = TotalPages;

			// set pager index links
			List<PagerIndex> indexs = new List<PagerIndex>();

			if(totalPages <= 25)
			{
				for(int i = 1; i <= totalPages; i++)
				{
					indexs.Add(new PagerIndex(i.ToString()));
				}
			}
			else
			{
				for(var i = 13; i > 0; i--)
				{
					if(indexs.Count >= 27)
					{
						break;
					}

					var index = currentPageIndex - i;

					if(index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}

				for(var i = 0; i <= 13; i++)
				{
					if(indexs.Count > 27)
					{
						break;
					}

					var index = currentPageIndex + i;

					if(index <= totalPages && index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}
			}

			this.RptrPageIndex.DataSource = indexs;
			this.RptrPageIndex.DataBind();

			this.LabelTotalPages.Text = totalPages.ToString();
			this.LabelTotalRecords.Text = TotalRecords.ToString();

			// disenabel the current pagerindex link
			if(RptrPageIndex.Items.Count > 0)
			{
				for(int i = 0; i < RptrPageIndex.Items.Count; i++)
				{
					LinkButton thePagerIndexLink = RptrPageIndex.Items[i].FindControl("LBPageIndex") as LinkButton;

					if(thePagerIndexLink.Text.Trim().Equals((currentPageIndex + 1).ToString()))
					{
						thePagerIndexLink.Enabled = false;
					}
				}
			}

			lbtnFirstPage.Enabled = (currentPageIndex > 0);

			lbtnLastPage.Enabled = totalPages > 1 && (currentPageIndex != totalPages - 1);

			var currentPageIndex1 = this.PageIndex1;
			var totalPages1 = TotalPages1;

			// set pager index links
			List<PagerIndex> indexs1 = new List<PagerIndex>();

			if(totalPages1 <= 25)
			{
				for(int i = 1; i <= totalPages1; i++)
				{
					indexs1.Add(new PagerIndex(i.ToString()));
				}
			}
			else
			{
				for(var i = 13; i > 0; i--)
				{
					if(indexs1.Count >= 27)
					{
						break;
					}

					var index1 = currentPageIndex1 - i;

					if(index1 > 0)
					{
						indexs1.Add(new PagerIndex(index1.ToString()));
					}
				}

				for(var i = 0; i <= 13; i++)
				{
					if(indexs1.Count > 27)
					{
						break;
					}

					var index = currentPageIndex1 + i;

					if(index <= totalPages1 && index > 0)
					{
						indexs1.Add(new PagerIndex(index.ToString()));
					}
				}
			}

			this.RptrPageIndex1.DataSource = indexs1;
			this.RptrPageIndex1.DataBind();

			this.LabelTotalPages1.Text = totalPages1.ToString();
			this.LabelTotalRecords1.Text = TotalRecords1.ToString();

			// disenabel the current pagerindex link
			if(RptrPageIndex1.Items.Count > 0)
			{
				for(int i = 0; i < RptrPageIndex1.Items.Count; i++)
				{
					LinkButton thePagerIndexLink1 = RptrPageIndex1.Items[i].FindControl("LBPageIndex1") as LinkButton;

					if(thePagerIndexLink1.Text.Trim().Equals((currentPageIndex1 + 1).ToString()))
					{
						thePagerIndexLink1.Enabled = false;
					}
				}
			}

			lbtnFirstPage1.Enabled = (currentPageIndex1 > 0);

			lbtnLastPage1.Enabled = totalPages1 > 1 && (currentPageIndex1 != totalPages1 - 1);
		}

		protected void lvMyVideos_ItemCommand(object sender, System.Web.UI.WebControls.ListViewCommandEventArgs e)
		{
			if(e.CommandName == "remove")
			{
				var id = int.Parse(e.CommandArgument.ToString());

				if(_VideoProvider == null)
				{
					_VideoProvider = new VideoProvider();
				}

				if(_VideoProvider.DeleteVideo(id))
				{
					//BindMyVideos();
				}
			}
		}

		public string ServerURL
		{
			get
			{
				if(Request.Url.Port > 0)
				{
					return string.Format("http://{0}:{1}", Request.Url.Host, Request.Url.Port.ToString());
				}
				else
				{
					return "http://" + Request.Url.Host;
				}
			}
		}

		protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.PageIndex = 0;
		}

		protected void LBPageIndex_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			switch(e.CommandName)
			{
				case "PageIndex":
					{
						LinkButton theLink = sender as LinkButton;

						int index = int.Parse(theLink.Text);

						PageIndex = index - 1;

						//TotalRecords = DataSource.Count();

						BindMyVideos();
					}
					break;
			}
		}

		protected void lbtnFirstPage_Click(object sender, EventArgs e)
		{
			PageIndex = 0;
			BindMyVideos();
		}

		protected void lbtnLastPage_Click(object sender, EventArgs e)
		{
			PageIndex = TotalPages - 1;
			BindMyVideos();
		}

		protected void ddlPageSize1_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.PageIndex1 = 0;
		}

		protected void LBPageIndex1_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			switch(e.CommandName)
			{
				case "PageIndex":
					{
						LinkButton theLink = sender as LinkButton;

						int index = int.Parse(theLink.Text);

						PageIndex1 = index - 1;

						//TotalRecords = DataSource.Count();

						BindPublicVideo();
					}
					break;
			}
		}

		protected void lbtnFirstPage1_Click(object sender, EventArgs e)
		{
			PageIndex1 = 0;
			BindPublicVideo();
		}

		protected void lbtnLastPage1_Click(object sender, EventArgs e)
		{
			PageIndex1 = TotalPages1 - 1;
			BindPublicVideo();
		}

		private void BindPublicVideo()
		{
			var data = DataSourse1;

			lvPublicVideos.DataSource = data;
			lvPublicVideos.DataBind();
		}

		private void BindMyVideos()
		{
			var data = DataSourse;

			lvMyVideos.DataSource = data;
			lvMyVideos.DataBind();
		}
	}
}